""" Handles access to the computer display.
    
    Aliases to Pygame functions defined here:
    
    get_screen  --> get_surface -->  pygame.display.get_surface()
    toggle_fullscreen -->  pygame.display.toggle_fullscreen()
    update --> pygame.display.update()
    flip --> pygame.display.flip()
    
    NOTE: Many modules require this module to be initialized before
    those modules are initialized themselves, most notably the `event`
    module.
    
    NOTE: The `set_mode` defined here is different than the one defined
    by `pygame.display`.
"""

import os

import pygame
from pygame import (
    FULLSCREEN,
    RESIZABLE,
)

# The constant NOBORDER seems to have changed to NOFRAME in newer versions of
# pygame. This code should deal with that problem if it is present.
try:
    from pygame import NOBORDER
    NOFRAME = NOBORDER
except ImportError:
    from pygame import NOFRAME
    NOBORDER = NOFRAME

from pygame.display import (
    toggle_fullscreen,
    get_surface,
    update,
    flip
)
get_screen = get_surface

import error
DEFAULT_RES = 640,480
DEFAULT_BPP = 32
DEFAULT_FULL = False

__all__ = (
    'init',
    'set_mode',
    
    # From pygame.display:
    
    'update',
    'get_screen'
    'toggle_fullscreen'
)

_init = False # Whether or not the module has been initialized

def init():
    """ Initializes the video system.
    
        This must be called before using any other function from
        this module.
    """
    global _init
    if not _init:
        _init = True
        pygame.display.init()

def set_mode(res=None, bpp=None, full=False, center=False, resizable=False,
             noframe=False):
    __doc__ =\
    """ Creates the application window and sets the video mode.
        
        res:    Screen resolution as ordered pair. This will be the
                size of the window or resolution of the screen
                depending on whether the game runs in windowed mode or
                full screen mode, respectively. Nearly any size of
                window will work, but resolutions supported will
                depend on the graphics card and monitor of the
                computer. The default resolution is %(DEFAULT_RES)s.
        
        bpp:    The number of bits per pixel of the screen surface.
                Supported values will depend on the system, but
                typically 8, 16, 24, or 32 will work. Default %(DEFAULT_BPP)i.
        
        full:   Whether or not to display in full screen mode.
                If false, the display is a window. If true, it is
                the whole screen. If this is true the screen resolution
                will be changed to the value of `res`. If that
                resolution is not supported an exception will occur.
                Default %(DEFAULT_FULL)s.
        
        center: Whether or not to center the window when it is first
                opened. The effect of this will not be visible unless
                `full` is False. Also, the window can still be
                moved even if it is initially centered. Default False.
        
        resizable:
                If True, the window will be resizable, and events will
                be generated when the user resizes the window. (See
                the Pygame event documentation.)
        
        noframe:
                If True, thw window will not have a frame. Be aware
                that this means there will not be a close button
                either, do you will have to utilize another form of
                input to make the app exit.
        
        Returns the screen surface.
    """ % {
        'DEFAULT_BPP':DEFAULT_BPP, 'DEFAULT_RES':DEFAULT_RES, 'DEFAULT_FULL':DEFAULT_FULL
    }
    
    if res is None: res = DEFAULT_RES
    if bpp is None: bpp = DEFAULT_BPP
    if full is None: full = DEFAULT_FULL
    
    if center:
        os.environ['SDL_VIDEO_CENTERED'] = '1'
    else:
        try:
            del os.environ['SDL_VIDEO_CENTERED']
        except KeyError:
            pass
    
    screen = pygame.display.set_mode(res,
        (FULLSCREEN if full else 0) |
        (RESIZABLE if resizable else 0) |
        (NOFRAME if noframe else 0),
        bpp
    )
    
    return screen
